package com.es.www.kyl.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author by michangtao
 * @Date 2023/3/24 11:09
 * @Description
 */
@Component
public class SpringContainerHolder implements ApplicationContextAware {

    public static ApplicationContext applicationContext;

    public SpringContainerHolder() {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContainerHolder.applicationContext = applicationContext;
    }

    public static String getActiveProfile() {
        return applicationContext.getEnvironment().getActiveProfiles()[0];
    }


    public static <T> T lookUp(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }

    public static Object lookUp(String name){
        return applicationContext.getBean(name);
    }


}
